#!/usr/bin/python
import sys, os, string, datetime
from Lib import Util
# Usage: convert_archives.py YYYY-MM-DD YYYY-MM-DD monitor
# start/end date and directory of the archive (in our case, the RV monitor name)

# bzcat the bz2 to stdout and pipe it to stdin for the route_btoa
# script. Redirct the stdout from route_btoa to the destination plain text
# Finally, bz2 the plain text file
#
# src_file : full path to binary formatted .bz2 archive (with .bz2 ext)
# dst_file : full path to plain text destination (without .bz2 ext)

def convert(src_file, dst_file):
    if os.path.exists(dst_file):
        os.unlink(dst_file)
    if os.path.exists('%s.bz2' % dst_file):
        os.unlink('%s.bz2' % dst_file)

    if '.bz2' in src_file:
        cat_cmd = bzcat
    elif '.gz' in src_file:
        cat_cmd = zcat
    else:
        raise
    
    os.system('nice %s %s | nice %s -m -i stdin > %s' % \
            (cat_cmd, src_file, route_btoa, dst_file))
    os.system('nice %s %s' % (bzip2, dst_file))


def convert_range(monitor, start_date, end_date):
    # Make sure we have data for the monitor
    monitor_dir = archive_dir + '/' + monitor
    if not os.path.exists(monitor_dir):
        return

    # Loop through each day from start to end
    # Get a list of all binary archives for that day and convert them
    current_month = 0
    current_date = start_date
    while current_date <= end_date:
        print "Converting archives for %04d-%02d-%02d in %s" % (current_date.year, current_date.month, current_date.day, monitor)
    
        # Started a new month, change directories
        if current_date.month != current_month:
            working_dir = '%s/%04d.%02d' % \
                (monitor_dir, current_date.year, current_date.month)
            os.chdir(working_dir + '/mrt')
            current_month = current_date.month

        # Find the archives for that day
        files = [f for f in os.listdir(working_dir + '/mrt') \
            if f.startswith('updates.%04d%02d%02d' % \
                (current_date.year, current_date.month, current_date.day))]

        # Convert them
        for f in files:
            if '.bz2' in f:
                convert(working_dir + '/mrt/' + f, working_dir + '/plain/' + f[0:-4])
            elif '.gz' in f:
                convert(working_dir + '/mrt/' + f, working_dir + '/plain/' + f[0:-3])
            else:
                raise
    
        # Go to the next day   
        current_date = current_date + datetime.timedelta(days=1)


#----Main----#
    
# Base dir for the archives
archive_dir = Util.archive_dir
bzcat = '/bin/bzcat'
zcat = '/bin/zcat'
bzip2 = '/bin/bzip2'
route_btoa = '/usr/local/bin/route_btoa'
assert os.path.exists(archive_dir)
assert os.path.exists(bzcat)
assert os.path.exists(bzip2)
assert os.path.exists(route_btoa)

if len(sys.argv) < 3:
    print "Usage: convert_archives.py YYYY-MM-DD YYYY-MM-DD [monitor]"
    sys.exit(-1)

# Convert the start/end dates into (year, month, day) lists
start = string.split(sys.argv[1], '-')
end = string.split(sys.argv[2], '-')
monitors = sys.argv[3:]
if not monitors:
    monitors = Util.monitors
if monitors[0] == 'rv':
    monitors = Util.rv_monitors
elif monitors[0] == 'ripe':
    monitors = Util.ripe_monitors


# Create the date objects
start_date = datetime.date(int(start[0]), int(start[1]), int(start[2]))
end_date = datetime.date(int(end[0]), int(end[1]), int(end[2]))

# Append the monitor directory and make sure it exists
for monitor in monitors:
    convert_range(monitor, start_date, end_date)
